home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP04 / CLOVER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  4.4 KB  |  123 lines

  1. /*--------------------------------------------------
  2.    CLOVER.C -- Clover Drawing Program using Regions
  3.                (c) Charles Petzold, 1996
  4.   --------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8.  
  9. #define TWO_PI (2.0 * 3.14159)
  10.  
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15.      {
  16.      static char szAppName[] = "Clover" ;
  17.      HWND        hwnd ;
  18.      MSG         msg ;
  19.      WNDCLASSEX  wndclass ;
  20.  
  21.      wndclass.cbSize        = sizeof (wndclass) ;
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = NULL ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  33.  
  34.      RegisterClassEx (&wndclass) ;
  35.  
  36.      hwnd = CreateWindow (szAppName, "Draw a Clover",
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.  
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.  
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.           {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.           }
  50.      return msg.wParam ;
  51.      }
  52.  
  53. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  54.      {
  55.      static HRGN hRgnClip ;
  56.      static int  cxClient, cyClient ;
  57.      double      fAngle, fRadius ;
  58.      HCURSOR     hCursor ;
  59.      HDC         hdc ;
  60.      HRGN        hRgnTemp[6] ;
  61.      int         i ;
  62.      PAINTSTRUCT ps ;
  63.  
  64.      switch (iMsg)
  65.           {
  66.           case WM_SIZE:
  67.                cxClient = LOWORD (lParam) ;
  68.                cyClient = HIWORD (lParam) ;
  69.  
  70.                hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  71.                ShowCursor (TRUE) ;
  72.  
  73.                if (hRgnClip)
  74.                     DeleteObject (hRgnClip) ;
  75.  
  76.                hRgnTemp[0] = CreateEllipticRgn (0, cyClient / 3,
  77.                                         cxClient / 2, 2 * cyClient / 3) ;
  78.                hRgnTemp[1] = CreateEllipticRgn (cxClient / 2, cyClient / 3,
  79.                                         cxClient, 2 * cyClient / 3) ;
  80.                hRgnTemp[2] = CreateEllipticRgn (cxClient / 3, 0,
  81.                                         2 * cxClient / 3, cyClient / 2) ;
  82.                hRgnTemp[3] = CreateEllipticRgn (cxClient / 3, cyClient / 2,
  83.                                         2 * cxClient / 3, cyClient) ;
  84.                hRgnTemp[4] = CreateRectRgn (0, 0, 1, 1) ;
  85.                hRgnTemp[5] = CreateRectRgn (0, 0, 1, 1) ;
  86.                hRgnClip    = CreateRectRgn (0, 0, 1, 1) ;
  87.  
  88.                CombineRgn (hRgnTemp[4], hRgnTemp[0], hRgnTemp[1], RGN_OR) ;
  89.                CombineRgn (hRgnTemp[5], hRgnTemp[2], hRgnTemp[3], RGN_OR) ;
  90.                CombineRgn (hRgnClip,    hRgnTemp[4], hRgnTemp[5], RGN_XOR) ;
  91.  
  92.                for (i = 0 ; i < 6 ; i++)
  93.                     DeleteObject (hRgnTemp[i]) ;
  94.  
  95.                SetCursor (hCursor) ;
  96.                ShowCursor (FALSE) ;
  97.                return 0 ;
  98.  
  99.           case WM_PAINT:
  100.                hdc = BeginPaint (hwnd, &ps) ;
  101.  
  102.                SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;
  103.                SelectClipRgn (hdc, hRgnClip) ;
  104.  
  105.                fRadius = _hypot (cxClient / 2.0, cyClient / 2.0) ;
  106.  
  107.                for (fAngle = 0.0 ; fAngle < TWO_PI ; fAngle += TWO_PI / 360)
  108.                     {
  109.                     MoveToEx (hdc, 0, 0, NULL) ;
  110.                     LineTo (hdc, (int) ( fRadius * cos (fAngle) + 0.5),
  111.                                  (int) (-fRadius * sin (fAngle) + 0.5)) ;
  112.                     }
  113.                EndPaint (hwnd, &ps) ;
  114.                return 0 ;
  115.  
  116.           case WM_DESTROY:
  117.                DeleteObject (hRgnClip) ;
  118.                PostQuitMessage (0) ;
  119.                return 0 ;
  120.           }
  121.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  122.      }
  123.